/**
 * Created with JetBrains WebStorm.
 * User: Amir
 * Date: 5/14/13
 * Time: 1:19 AM
 * To change this template use File | Settings | File Templates.
 */
var genre_search_url="https://www.googleapis.com/freebase/v1/search?limit=10&indent=true&filter=(any+type%3A%2Fmusic%2Fgenre)&query=";

var genre = new Object();

function appendResults(item) {
    var results = $('#results');
    results.append("<div class=\"video_details\">"+ item.name + "</div>");

}

function find_videos( srch_genre) {
    $.ajax({
        url: genre_search_url + srch_genre,
        success: function(data){
            console.log("success");
            console.log(data);
            data.result.forEach( function (item){
                genre[item.name] = item.mid;
                appendResults(item);

            })

        }


    });
}

function load() {
    console.log("load");
    gapi.client.setApiKey("AIzaSyDq2-Zw09eM51mq6Fx6ezvRm958v5pqrGg");
    console.log("load api");
    gapi.client.load('youtube', 'v3', function() {});
    console.log( "youtube ai loaded");
    $("#genre_button").on("click", function(e){
        console.log(e);
        var v = $("#genre").val();
        console.log( "clicked "  + v);
        $("#results").html("");
        find_videos(v);
    });
    console.log("end load");

}

function search() {
    var q = $('#query').val();
    var request = gapi.client.youtube.search.list({
        q: q,
        part: 'snippet'
    });

    request.execute(function(response) {
        var str = JSON.stringify(response.result);
        $('#search-container').html('<pre>' + str + '</pre>');
    });
}